package projects;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;

@SuppressWarnings("serial")
public class issue implements Serializable{
	private Integer taskId;
	private String issueDescription;
	private Date creationDate;
	
	
	
	public Integer getTaskId() {
		return taskId;
	}

	public void setTaskId(Integer taskId) {
		this.taskId = taskId;
	}

	public String getIssueDescription() {
		return issueDescription;
	}

	public void setIssueDescription(String issueDescription) {
		this.issueDescription = issueDescription;
	}

	public String getCreationDate() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");
		return format.format(creationDate);
	}

//------------------------------> create's a new issue for a given task <-------------------------------------------------------------------------------	
	
	public boolean createIssue(Integer id){
		Connection con = null;
		boolean isExecuted =false;
		try{
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection("jdbc:mysql://localhost:8889/projectsDb","root", "root");
			
			PreparedStatement createIssue = (PreparedStatement) con.prepareStatement("insert into issues(taskId, issueDescription, createdBy) values (?, ?, ?)");
			createIssue.setInt(1, this.getTaskId());
			createIssue.setString(2, this.getIssueDescription());
			createIssue.setInt(3, id);
			Integer result = createIssue.executeUpdate();
			
			if (result == 1){
				isExecuted = true;
				return isExecuted;
			}
		}
		catch (Exception e){
			System.out.println(e.getMessage());
		}
		finally{
			try {
				if(con != null)
					con.close();
			} 
			catch(Exception e) {}
		}
		return isExecuted;
	}
	
	@SuppressWarnings("rawtypes")
	public Hashtable readIssue(){
		Connection con = null;
		Hashtable<Integer, ArrayList<String>> issue = new Hashtable<Integer, ArrayList<String>>();
		try{
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection("jdbc:mysql://localhost:8889/projectsDb","root", "root");
			
			PreparedStatement addTeamMember = (PreparedStatement) con.prepareStatement("select issueId, issueDescription, createdBy, creationDate from issues where taskId = ?");
			addTeamMember.setInt(1, this.getTaskId());
			ResultSet rs = addTeamMember.executeQuery();
			
			while (rs.next()){
				ArrayList<String> issueInfo = new ArrayList<String>();
				issueInfo.add(rs.getString(2));
				issueInfo.add(rs.getString(3));
				issueInfo.add(rs.getString(4));
				issue.put(rs.getInt(1), issueInfo);
			}
		}
		catch (Exception e){
			System.out.println(e.getMessage());
		}
		finally{
			try {
				if(con != null)
					con.close();
			} 
			catch(Exception e) {}
		}
		return issue;
	}
	
	public static void main(String args[]){
		issue x = new issue();
		x.setTaskId(7);
//		x.setIssueDescription("issueDescription 1");
//		x.createIssue();
		System.out.println(x.readIssue());
	}

}
